package com.xjc.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xjc.business.entity.domain.Activities;
import com.xjc.business.entity.domain.ActivityComments;
import com.xjc.business.entity.domain.ActivityJoinLists;
import com.xjc.business.entity.po.stu.ActivityCommentsPo;
import com.xjc.business.entity.vo.stu.ActivityCommentsVo;
import com.xjc.business.service.*;
import com.xjc.business.mapper.ActivityCommentsMapper;
import com.xjc.system.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author root
* @description 针对表【activity_comments(宿舍评优评价表)】的数据库操作Service实现
* @createDate 2025-01-25 10:24:30
*/
@Service
public class ActivityCommentsServiceImpl extends ServiceImpl<ActivityCommentsMapper, ActivityComments>
    implements ActivityCommentsService{

    @Autowired
    @Lazy
    StudentsService studentsService;

    @Autowired
    @Lazy
    WxUserService wxUserService;

    @Autowired
    @Lazy
    ActivityJoinListsService activityJoinListsService;

    /**
     * 获取宿舍评优活动评论列表
     * @param id 宿舍评优活动参加记录id
     * @return Result
     */
    @Override
    public Result<List<ActivityCommentsVo>> getActivityCommentsList(Integer id) {

        LambdaQueryWrapper<ActivityComments> activityCommentsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        activityCommentsLambdaQueryWrapper.eq(ActivityComments::getJoinId, id);
        List<ActivityCommentsVo> activityCommentsVoList = list(activityCommentsLambdaQueryWrapper).stream().map(activityComments -> {
            ActivityCommentsVo activityCommentsVo = new ActivityCommentsVo();

            String name = studentsService.getById(activityComments.getCommentId()).getName();
            activityCommentsVo.setCommentStuName(name);
            activityCommentsVo.setType(activityComments.getType());
            activityCommentsVo.setReason(activityComments.getReason());
            activityCommentsVo.setIsAnonymity(activityComments.getIsAnonymity());
            activityCommentsVo.setAnonymityName(activityComments.getAnonymityName());
            activityCommentsVo.setCreateTime(activityComments.getCreateTime());
            return activityCommentsVo;
        }).collect(Collectors.toList());

        return Result.okWithData(activityCommentsVoList);
    }

    /**
     * 添加宿舍评优活动评论
     *
     * @param activityCommentsPo 评论内容
     * @return Result
     */
    @Override
    @Transactional
    public Result<String> addComment(ActivityCommentsPo activityCommentsPo) {

        Integer stuId = wxUserService.getById(activityCommentsPo.getWxId()).getStuId();
        Integer joinId = activityCommentsPo.getJoinId();

        LambdaQueryWrapper<ActivityComments> activityCommentsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        activityCommentsLambdaQueryWrapper.eq(ActivityComments::getJoinId,joinId);
        activityCommentsLambdaQueryWrapper.eq(ActivityComments::getCommentId,stuId);
        if (count(activityCommentsLambdaQueryWrapper) > 0){
            return Result.ok("你已经表态过，请勿重复参与");
        }

        ActivityComments activityComments = new ActivityComments();
        activityComments.setJoinId(joinId);
        activityComments.setCommentId(stuId);
        Integer type = activityCommentsPo.getType();
        activityComments.setType(type);
        activityComments.setReason(activityCommentsPo.getReason());
        activityComments.setIsAnonymity(activityCommentsPo.getIsAnonymity());
        activityComments.setAnonymityName(activityCommentsPo.getAnonymityName());
        activityComments.setCreateTime(new Date());
        activityComments.setUpdateTime(new Date());
        Integer activityId = activityCommentsPo.getActivityId();
        activityComments.setActivityId(activityId);
        save(activityComments);

        synchronized (this){
            ActivityJoinLists activityJoinLists = activityJoinListsService.getById(joinId);
            if(type == 0){
                activityJoinLists.setUnlikeNum(activityJoinLists.getUnlikeNum()+1);
            } else if (type == 1) {
                activityJoinLists.setLikeNum(activityJoinLists.getLikeNum()+1);
            }
            activityJoinListsService.updateById(activityJoinLists);
        }

        return Result.okWithData("表态成功");
    }
}




